package com.jl.scr;

import opencard.core.terminal.APDU;
import opencard.core.terminal.CommandAPDU;

import com.jl.util.HexUtil;

public class CMD extends SCR {
	private APDU apdu;
	private String excep = "";
	private String sw = "";

	private final static String CMD = "CMD ";
	private final static String LRectangle = "[";
	private final static String RRectangle = "]";
	private final static String L = "(";
	private final static String R = ")";
	private final static String ENDL = "\n";

	public CMD(APDU apdu) {
		this.apdu = new CommandAPDU(apdu.getBytes());
	}

	public CMD(APDU apdu, String excep, String sw) {
		this.apdu = apdu;
		if (excep != null) {
			this.excep = HexUtil.format(excep).trim();
		} else {
			excep = "";
		}
		if (sw != null) {
			this.sw = sw.trim();
		} else {
			this.sw = "";
		}
	}

	public CMD(APDU apdu, String sw) {
		this.apdu = apdu;
		this.excep = "";
		this.sw = sw.trim();
	}

	public void setExcept(String data) {
		if (data == null) {
			this.excep = "";
		}
		this.excep = data.trim();
	}

	public String getExcept() {
		if (this.excep == null) {
			return "";
		} else {
			return this.excep;
		}
	}

	public void setSW(String sw) {
		if (sw == null) {
			this.sw = "";
		}
		this.sw = sw.trim();
	}

	public String getSW() {
		if (this.sw == null) {
			return "";
		} else {
			return this.sw;
		}
	}

	public APDU getApdu() {
		return this.apdu;
	}

	public void setApdu(APDU apdu) {
		this.apdu = apdu;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(CMD).append(HexUtil.toHexString(apdu.getBytes()));
		if (excep != null && !excep.equals("")) {
			sb.append(LRectangle).append(HexUtil.format(excep)).append(RRectangle);
		}
		if (sw != null && !sw.equals("")) {
			sb.append(L).append(sw).append(R);
		}
		return sb.append(ENDL).toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		} else if (obj == this) {
			return true;
		} else if (!(obj instanceof CMD)) {
			return false;
		} else {
			CMD rem = (CMD) obj; 
			return rem.toString().equalsIgnoreCase(this.toString());
		}
	}

	@Override
	public int hashCode() {
		return this.toString().hashCode();
	}

	public static CMD parse(String cmd) {
//		cmd = cmd.trim();
//		int i = cmd.indexOf(' ');
//		int j = cmd.indexOf('[', i);
//		if (j == -1) {
//			int n = cmd.indexOf('(', i);
//			if (n == -1) {
//				return new CMD(new APDU(cmd.substring(i)));
//			} else {
//				int x = cmd.indexOf(')', n);
//				return new CMD(new APDU(cmd.substring(i, n)), cmd.substring(n + 1, x));
//			}
//		} else {
//			int k = cmd.indexOf(']', j);
//			int l = cmd.indexOf('(', k);
//			if (l == -1) {
//				return new CMD(new APDU(cmd.substring(i, j)), cmd.substring(j + 1, k), "");
//			} else {
//				int m = cmd.indexOf(')', l);
//				return new CMD(new APDU(cmd.substring(i, j)), cmd.substring(j + 1, k), cmd.substring(l + 1, m));
//			}
//		}
		
		//TODO : not implements parse cmd
		return null;

	}

}
